/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import static br.com.fatecpg.songs.intranet.managedbeans.RegisterManagedBean.LIST_STATE;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.Musician;
import br.com.fatecpg.songs.entity.Song;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Eloyza
 */
@ManagedBean
@ViewScoped
public class SongManagedBean implements RegisterManagedBean<Song> {

    private Song song;
    private RegisterFacade<Song> facade = new RegisterFacade<Song>(Song.class);  
    private int viewState;
    private Long[] musiciansId;
    private List<Song> list;

    public SongManagedBean() throws DAOException {
        song = new Song();        
        list = facade.list();
        this.viewState = LIST_STATE;
    }           
       
    public Long[] getMusiciansId() {
        return musiciansId;
    }   

    @Override
    public void openNew() {
        song = new Song();
        musiciansId = null;
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException {
        song = facade.load();
        setMusicians(song.getComposers());
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void delete() throws DAOException {
        facade.delete();
        list = facade.list();
        this.viewState = LIST_STATE;
    }

    @Override
    public void register() throws DAOException {
        facade.setUpdateInfo(song);
        song.setComposers(getMusicians());
        facade.register(song);
        list = facade.list();
        viewState = LIST_STATE;
    }
    
    @Override
    public void cancel() {
        this.viewState = LIST_STATE;
    }
    
    public List<Musician> getMusicians() {
        List<Musician> musicians = new ArrayList<Musician>();
        for (Long musiciansId1 : musiciansId) {
            Musician musician = new Musician();
            musician.setId(musiciansId1);
            musicians.add(musician);
        }
        return musicians;
    }
    
    private void setMusicians(List<Musician> musicians) {
        musiciansId = new Long[musicians.size()];
        for (int i = 0; i < musicians.size(); i++) {
            musiciansId[i] = musicians.get(i).getId();
        }
    }                
    
    @Override
    public List<Song> getList() {
        return list;
    }
    
    public Song getSong() {
        return song;
    }

    public void setSong(Song song) {
        this.song = song;
    }

    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }

    public void setMusiciansId(Long[] musiciansId) {
        this.musiciansId = musiciansId;
    }
    
}
